import { request } from "../../request/index.js";

// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_info: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const user_info = wx.getStorageSync('user_info');
    if (!user_info) {
      wx.showModal({
        title: '您未登录',
        content: '是否跳转到登录界面？',
        success(res){
          if(res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        }
      })
    }
    this.setData({
      user_info: user_info
    })
  },

  handleConfirm() {
    const data = this.data.user_info;
    var that = this;
    wx.request({
      url: 'http://47.94.130.28:8000/user/userinfoset/',
      data: data,
      method: "POST",
      success (res) {
        console.log(res)
        if (res.data.valid) {
          wx.showToast({
            title: '信息修改成功',
            icon: 'success',
          })
        } else {
          wx.showModal({
            title: "信息修改失败",
            content: res.data.message,
            showCancel: false
          })
        }
        // save changed information to local storage
        wx.setStorageSync('user_info', that.data.user_info)
      }
    })
    // const valid = false;
    // const message = "网络错误，请稍后重试";
    // if (valid) {
    //   wx.showToast({
    //     title: '信息修改成功',
    //     icon: 'success',
    //   })
    // } else {
    //   wx.showModal({
    //     title: "信息修改失败",
    //     content: message,
    //     showCancel: false
    //   })
    // }
    // wx.setStorageSync('user_info', that.data.user_info)
  },

  handlePasswd() {
    wx.navigateTo({
      url: "/pages/passwd/passwd"
    })
  },

  handleNickname(e) {
    let user_info = this.data.user_info;
    user_info.user_name = e.detail.value;
    this.setData({
      user_info: user_info
    })
  },
  handleTel(e) {
    let user_info = this.data.user_info;
    user_info.user_phone = e.detail.value;
    this.setData({
      user_info: user_info
    })
  },
  handleMail(e) {
    let user_info = this.data.user_info;
    user_info.user_email = e.detail.value;
    this.setData({
      user_info: user_info
    })
  },
  handleWechat(e) {
    let user_info = this.data.user_info;
    user_info.user_vxid = e.detail.value;
    this.setData({
      user_info: user_info
    })
  }
})